package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC822 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int flipgame(int[] fronts, int[] backs) {
            int len = fronts.length;
            TreeSet<Integer> candidateSet = new TreeSet<>();
            TreeSet<Integer> cannotSet = new TreeSet<>();

            for (int i = 0; i < len; i++) {
                if (fronts[i] == backs[i]) {
                    cannotSet.add(fronts[i]);
                }
            }

            for (int i = 0; i < len; i++) {
                if (fronts[i] != backs[i]) {
                    if (!cannotSet.contains(fronts[i])) candidateSet.add(fronts[i]);
                    if (!cannotSet.contains(backs[i])) candidateSet.add(backs[i]);
                }
            }

            return candidateSet.size() <= 0 ? 0 : candidateSet.first();
        }
    }

    public void run() {
        Solution solution = new Solution();
//        int[] front = {1, 2, 4, 4, 7};
//        int[] back = {1, 3, 4, 1, 3};

//        int[] front = {1};
//        int[] back = {1};

        int[] front = {1, 1};
        int[] back = {2, 1};

        System.out.println(solution.flipgame(front, back));
    }

    public static void main(String[] args) throws Exception {
        LC822 an = new LC822();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
